package demo_tortoise_and_hare;

public class Race implements Runnable{
    private  String winner;
    @Override
    public void run() {
        for (int i = 1; i <=100; i++) {
            boolean flag=isGameOver(i);
            if (flag) {
                break;
            }
            else System.out.println(Thread.currentThread().getName()+"跑了-->"+i+"步");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private synchronized boolean isGameOver(int i) {
        if(winner!=null)
            return true;
        if(i>=100){
            winner=Thread.currentThread().getName();
            System.out.println(winner+"赢得了比赛");
            return true;
        }
        return false;
    }
}
